package com.nit.store.controller;


import com.nit.store.service.UploadService;
import com.nit.store.util.JwtUtils;
import com.nit.store.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/users")
@Slf4j
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private JwtUtils jwtUtils;



    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @PreAuthorize("hasAnyAuthority('/user/update')")
    @RequestMapping("/avatar/change")
    public JsonResult<Map> updateAvatar(MultipartFile file, @RequestHeader(value="Authentication")String token){
        String jwt = token.substring(tokenHeader.length());
        Long uid = jwtUtils.parseJwtGetId(jwt);
        Map map = uploadService.updateAvatar(file, uid);
        return JsonResult.ok(map);
    }

}
